package com.baidu.ota.activity;

import android.Manifest;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import com.baidu.commonlib.interfaces.IOtaAgent;
import com.baidu.ota.application.BaseApplication;
import com.baidu.ota.interfaces.IActivityProxy;
import com.baidu.ota.interfaces.IBindListener;

import java.util.List;

/**
 * manifest中注册的Activity
 */
public class LaunchActivity extends AppCompatActivity implements IBindListener {

    private IActivityProxy proxy;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // todo
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]{Manifest.permission.CHANGE_NETWORK_STATE
                    , Manifest.permission.WRITE_SETTINGS
                    , Manifest.permission.READ_PHONE_STATE}, 0);
        }
        proxy = BaseApplication.getInst().getActivityProxy(this);
        proxy.onCreate();
    }

    @Override
    protected void onStart() {
        super.onStart();
        proxy.onStart();
        BaseApplication.getInst().registerListener(this);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        proxy.onRestart();
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        proxy.onWindowFocusChanged(hasFocus);
    }

    @Override
    protected void onStop() {
        super.onStop();
        proxy.onStop();
        BaseApplication.getInst().unRegisterListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        proxy.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        proxy.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        proxy.onDestroy();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        proxy.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onBind(List<IOtaAgent> agents) {
        proxy.onBind(agents);
    }
}
